package com.ffcs.cn.action.base.treeTable;

import com.supermap.desktop.core.utilties.StringUtilities;
import org.jetbrains.userdefine.ui.speedSearch.SpeedSearchUtil;
import org.jetbrains.userdefine.ui.speedSearch.TreeSpeedSearch;
import org.pushingpixels.substance.api.renderers.SubstanceDefaultTreeCellRenderer;

import javax.swing.*;
import java.awt.*;

/**
  * 负责渲染第一列，即树节点列，这里返回的渲染控件是PanelCheck，PanelCheck里有三个控件，从左向右，依次是复选框、文本、图片，图片会根据showOperatorIcon来确定是否显示，其中复选框、图片可点击
 */
public class DemoJTreeTableTreeCellRenderCheck extends SubstanceDefaultTreeCellRenderer {

	// 最后一个图片是否显示
	private boolean showOperatorIcon = false;
	// 设置快速搜索器
	private TreeSpeedSearch treeSpeedSearch;

	public void setTreeSpeedSearch(TreeSpeedSearch treeSpeedSearch) {
		this.treeSpeedSearch = treeSpeedSearch;
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
		if (value instanceof TreeTableNode) {
			TreeTableNode treeTableNode = (TreeTableNode) value;
			TreeTableData treeTableData = treeTableNode.getTreeTableData();
			PanelCheck panelCheck = (PanelCheck) treeTableNode.getRenderComponent(0);
			panelCheck.setLabelImageVisible(this.showOperatorIcon);


			if (isSupportSpeedSearch() ) {
				// 根据搜索文本框内容高亮搜索匹配的内容，得在设置渲染文本控件内容后去高亮
				String searchText = TreeTableNode.getSearchText();
				if (!StringUtilities.isNullOrEmpty(searchText) && treeSpeedSearch != null && treeTableData.getValue() != null) {
					SpeedSearchUtil.applySpeedSearchHighlighting(treeSpeedSearch, panelCheck.getLabelText(), searchText, sel);
				} else {
					SpeedSearchUtil.applySpeedSearchHighlighting(tree, panelCheck.getLabelText(), true, sel);
				}
			}
			return panelCheck;
		}
		return null;
	}

	public void setShowOperatorIcon(boolean showOperatorIcon) {
		this.showOperatorIcon = showOperatorIcon;
	}
}
